package com.verisette.ncos.web.approver.action;

import java.io.InputStream;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.corp.agi.common.bean.PagingBean;
import org.corp.agi.service.bean.ServiceRequest;
import org.corp.agi.service.bean.ServiceResponse;
import org.corp.agi.web.action.BaseAction;
import org.corp.agi.web.util.ServiceRequestFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.Action;
import com.verisette.ncos.common.ProjectConstant;
import com.verisette.ncos.persistence.domain.CustomCustomerOrder;
import com.verisette.ncos.persistence.domain.TransCustomerOrder;
import com.verisette.ncos.service.approver.gateway.ApproverService;
import com.verisette.ncos.web.utilities.DateUtils;

@Component
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class CardPrintAction extends BaseAction 
{
	private Logger LOG = Logger.getLogger(CardPrintAction.class);
	
	//page action
	private final String PRINT_PREVIEW = "printPreview";
	private final String PRINT_SUCCESS = "printSuccess";
	
	private ApproverService approverService;
	@Autowired
	@Required
	public void setApproverService(ApproverService approverService) {
		this.approverService = approverService;
	}
	
	private List<TransCustomerOrder> customerOrderList;
	private PagingBean pagingBean;
	private String orderId;
	private TransCustomerOrder custOrder;
	private String pdfPath;
	
	private String sentDate;
	
	private InputStream inputStream;
	
	private String searchStatus;
	private String reason;
	
	public String init() throws Exception 
	{
		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing init()...");
		}

		httpServletRequest.getSession().setAttribute(ProjectConstant.SESSION_MENU_PAGE, ProjectConstant.MENU_PAGE_PRINTER);
		if(searchStatus == null) {
			searchStatus = String.valueOf(ProjectConstant.STATUS_WAIT_PRINT);
		}
		
		httpServletRequest.getSession().setAttribute(ProjectConstant.SESSION_SEARCH_ORDER_STATUS, searchStatus);
		pagingBean = new PagingBean(ProjectConstant.ROW_PER_PAGE);
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		
		req.setPaging(pagingBean);
		TransCustomerOrder searchOrder = new TransCustomerOrder();
		if(StringUtils.isNotBlank(searchStatus)){
			searchOrder.setStatus(new Integer(searchStatus));
		}
		req.setValueObject(searchOrder);
		
		ServiceResponse<List<TransCustomerOrder>> serviceResponse = approverService.inquiryTransCustomerOrder(req);
		
		if(isServiceResponseSuccess(serviceResponse))
		{
			customerOrderList = serviceResponse.getValueObject();
			pagingBean = serviceResponse.getPaging();
			
			if(null != customerOrderList && customerOrderList.size()>0)
			{
				custOrder = customerOrderList.get(0);
			}
		}
		
		return Action.SUCCESS;
	}
	
	public String inquiry()
	{
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		String sessSearchStatus = (String) httpServletRequest.getSession().getAttribute(ProjectConstant.SESSION_SEARCH_ORDER_STATUS);
		TransCustomerOrder searchOrder = new TransCustomerOrder();
		if(StringUtils.isNotBlank(sessSearchStatus)) {
			searchOrder.setStatus(new Integer(sessSearchStatus));
		}
		req.setValueObject(searchOrder);
		req.setPaging(pagingBean);
		
		ServiceResponse<List<TransCustomerOrder>> serviceResponse = approverService.inquiryTransCustomerOrder(req);
		
		if(isServiceResponseSuccess(serviceResponse))
		{
			customerOrderList = serviceResponse.getValueObject();
			pagingBean = serviceResponse.getPaging();
		}
		
		return Action.SUCCESS;
	}
	
	public String preview(){

		if(LOG.isDebugEnabled())
		{
			LOG.debug("Processing preview()...");
		}
		
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		TransCustomerOrder example = new TransCustomerOrder();
		example.setOrderId(new Integer(orderId));
		req.setValueObject(example);
		
		ServiceResponse<TransCustomerOrder> res = approverService.getTransCustomerOrder(req);
		
		if(isServiceResponseSuccess(res))
		{
			custOrder = res.getValueObject();
			if(LOG.isDebugEnabled()) LOG.debug(custOrder);
			

			int amount = 2;
			if(custOrder.getQuantity().intValue()>300){
				amount=3;
			}
			Date sentDate2 = DateUtils.plusWeekDay(new Date(), amount);
			this.sentDate = new SimpleDateFormat("dd/MM/yyyy", new Locale("th", "TH")).format(sentDate2);
			System.out.println(sentDate2);
		}
		
		return PRINT_PREVIEW;
	}
	
	public String print() throws Exception
	{
		if(LOG.isDebugEnabled()){
			LOG.debug(getClass().getSimpleName() + ".print()");
		}
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		TransCustomerOrder order = new TransCustomerOrder();
		order.setOrderId(Integer.valueOf(orderId));
		order.setStatus(ProjectConstant.STATUS_PRINT);
		order.setSentDate(new Timestamp(System.currentTimeMillis()));
		req.setValueObject(order);
		
		ServiceResponse<TransCustomerOrder> res = approverService.submitOrderPrint(req);
		if(isServiceResponseSuccess(res))
		{
			custOrder = res.getValueObject();
			pdfPath = null;
		}
		
		return PRINT_SUCCESS;
	}
	
	public String cancel() throws Exception
	{
		if(LOG.isDebugEnabled()){
			LOG.debug(getClass().getSimpleName() + ".cancel()");
		}
		ServiceRequest req = ServiceRequestFactory.getNewServiceRequest(httpServletRequest);
		TransCustomerOrder order = new TransCustomerOrder();
		order.setOrderId(Integer.valueOf(orderId));
		order.setNotApprovedReason(reason);
		order.setStatus(ProjectConstant.STATUS_PRINT_CANCEL);
		order.setSentDate(new Timestamp(System.currentTimeMillis()));
		req.setValueObject(order);
		
		ServiceResponse<TransCustomerOrder> res = approverService.submitOrderPrint(req);
		if(isServiceResponseSuccess(res))
		{
			custOrder = res.getValueObject();
			pdfPath = null;
		}
		
		return PRINT_SUCCESS;
	}
	
	public String back()
	{
		String sessSearchStatus = (String) httpServletRequest.getSession().getAttribute(ProjectConstant.SESSION_SEARCH_ORDER_STATUS);
		if(sessSearchStatus!=null && sessSearchStatus.equals(ProjectConstant.STATUS_WAIT_PRINT)) {
			if("Y".equals(httpServletRequest.getParameter("approveCompletePage"))){
				if(pagingBean.getPageCount() == pagingBean.getCurrentPage() && (pagingBean.getTotalRows() % ProjectConstant.ROW_PER_PAGE) == 1){
					if(pagingBean.getPageCount()>1) pagingBean.setCurrentPage(pagingBean.getCurrentPage()-1);
				}
			}
		}
		return inquiry();
	}
	
	public List<TransCustomerOrder> getCustomerOrderList() {
		return customerOrderList;
	}

	public void setCustomerOrderList(List<TransCustomerOrder> customerOrderList) {
		this.customerOrderList = customerOrderList;
	}

	public PagingBean getPagingBean() {
		return pagingBean;
	}

	public void setPagingBean(PagingBean pagingBean) {
		this.pagingBean = pagingBean;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public TransCustomerOrder getCustOrder() {
		return custOrder;
	}

	public void setCustOrder(TransCustomerOrder custOrder) {
		this.custOrder = custOrder;
	}

	public String getPdfPath() {
		return pdfPath;
	}

	public void setPdfPath(String pdfPath) {
		this.pdfPath = pdfPath;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getSentDate() {
		return sentDate;
	}

	public void setSentDate(String sentDate) {
		this.sentDate = sentDate;
	}
	
	public String getSearchStatus() {
		return searchStatus;
	}

	public void setSearchStatus(String searchStatus) {
		this.searchStatus = searchStatus;
	}
	
}
